package com.singing.manager;

import com.singing.dao.SingerDao;
import com.singing.model.Command;
import com.singing.model.Singer;

import java.util.List;
import java.util.Scanner;

public class SingerAddManager extends BaseManager {

    // 添加歌手
    private static final int INPUT_DETAIL = 1;

    SingerDao singerDao = new SingerDao();

    public SingerAddManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected int processCommand(int command) {
        switch (command) {
            case INPUT_DETAIL:
                showInputDetail();
                break;
        }
        return command;
    }

    private void showInputDetail() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入歌手的详细信息：");
        System.out.println("歌手名：");
        String name = scanner.next();
        Integer gender = null;
        while (true) {
            System.out.println("歌手性别(0-男，1-女)：");
            String genderStr = scanner.next();
            gender = null;
            try {
                gender = Integer.parseInt(genderStr);

                // 判断性别是否合法
                if (gender != 0 && gender != 1) {
                    throw new NumberFormatException();
                }

                break;

            } catch (NumberFormatException e) {
                e.printStackTrace();
                System.out.println("歌手性别输入错误，请重新输入");
            }
        }

        int insert = singerDao.insert(new Singer(name, gender));
        if (insert == 1) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }


}
